package com.shangxia.leetcode.greedyAlgorithm;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;

/**
 * @ProjectName: leetcode
 * @ClassName: Candy135
 * @Time: 2020/12/3
 * @Author: zhouy
 * @Description: 135. 分发糖果
 * @Experience:
 */

@Slf4j
public class Candy135 {
    public static void main(String[] args) {
        int[] s = {1, 0,2};
        System.out.println(candy(s));
    }

    public static int candy(int[] ratings) {
        int[] candies = new int[ratings.length];
        Arrays.fill(candies, 1);
        for (int i = 1; i < ratings.length; i++) {
            if (ratings[i] > ratings[i - 1]) {
                candies[i] = candies[i - 1] + 1;
            }
        }
        int sum = candies[ratings.length - 1];
        for (int i = ratings.length - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                candies[i] = Math.max(candies[i], candies[i + 1] + 1);
            }
            sum += candies[i];
        }
        return sum;
    }
}
